USE [BMIG_Mensual_POST_CADENA]
GO

/****** Object:  StoredProcedure [dbo].[WASP_M0002_VIGENTE]    Script Date: 11/24/2011 14:38:32 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[WASP_M0002_VIGENTE]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[WASP_M0002_VIGENTE]
GO

USE [BMIG_Mensual_POST_CADENA]
GO

/****** Object:  StoredProcedure [dbo].[WASP_M0002_VIGENTE]    Script Date: 11/24/2011 14:38:32 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[WASP_M0002_VIGENTE] 
AS 
BEGIN 
	SELECT	aooper AS oper
			,aosuc AS suc
			,aocta AS cta
			,cre.aostat
			,vigente = 
			CASE 
				WHEN oper IS NULL AND cre.aostat NOT IN (98,99) THEN 0
				WHEN cre.aostat IN (98,99) THEN 1
				WHEN oper IS NOT NULL AND cre.aostat NOT IN (98,99) THEN 2
			END 
	INTO	dbo.vigente 
	FROM	fsd010 cre LEFT JOIN mora ON aooper = oper and aosuc = suc and aocta = cta 
	WHERE	aosbop = 0
	AND		aotasa <> 0 
	AND		NOT EXISTS (
			SELECT	*
			FROM	as_desistidos AS anu
			WHERE	cre.aocta = anu.hcta
			AND		cre.aooper = anu.hoper
			AND		cre.aosuc = anu.hsucur 
			)
	ORDER BY vigente
END

GO


